const api = require('../../utils/api.js');

Page({
  data: {
    contactInfo: {
      phone: '',
      workTime: '',
      email: '',
      address: ''
    }
  },

  onLoad() {
    // 页面加载时获取联系信息
    this.loadContactInfo();
  },

  // 加载联系信息
  async loadContactInfo() {
    try {
      const response = await api.getContactInfo();
      const contactInfo = response.contact_info || {};
      
      this.setData({
        contactInfo: {
          phone: contactInfo.phone || '',
          workTime: contactInfo.work_time || '',
          email: contactInfo.email || '',
          address: contactInfo.address || ''
        }
      });
    } catch (error) {
      console.error('获取联系信息失败:', error);
      wx.showToast({
        title: '获取联系信息失败',
        icon: 'none'
      });
    }
  },

  // 拨打电话
  makeCall() {
    const phone = this.data.contactInfo.phone.replace(/-/g, '');
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: () => {
        wx.showToast({
          title: '拨号失败',
          icon: 'none'
        });
      }
    });
  },

  // 复制邮箱
  copyEmail() {
    wx.setClipboardData({
      data: this.data.contactInfo.email,
      success: () => {
        wx.showToast({
          title: '邮箱已复制',
          icon: 'success'
        });
      }
    });
  }
});